package com.ericsson.common.date;

import java.util.*;

import com.ericsson.common.exception.SmafException;

/**
 * <p>Title: ���õ�����ʱ�����</p>
 * <p>Description: ʵ�ֵõ�ĳ��ʱ�̵�������ʱ����ĸ��ָ�ʽ���ַ�</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Softtone Corporation</p>
 * @author Elaine Sun
 * @version 1.0
 */
public class AppliedDate
{
	/**
	 * ���캯��ǰʱ�̵Ķ���
	 */
	public AppliedDate()
	{
		Calendar theTime = Calendar.getInstance();
		set(theTime);
	}
	public AppliedDate(String time) throws SmafException
	{
			this();
			set(time);
	}

	static public AppliedDate getInstance()
	{
		return new AppliedDate();
	}

	/**
	 * ����������ʱ����
	 * @param year ��
	 * @param month ��
	 * @param day ��
	 * @param hour ʱ
	 * @param minute ��
	 * @param second ��
	 */
	public void set(int year, int month, int day, int hour, int minute, int second)
	{
		Calendar theTime = Calendar.getInstance();
		theTime.set(year, month - 1, day, hour, minute, second);
		set(theTime);
	}

	/**
	 * ����������ʱ�֣��벻��
	 * @param year ��
	 * @param month ��
	 * @param day ��
	 * @param hour ʱ
	 * @param minute ��
	 */
	public void set(int year, int month, int day, int hour, int minute)
	{
		Calendar theTime = Calendar.getInstance();
		theTime.set(year, month - 1, day, hour, minute);
		set(theTime);
	}

	/**
	 * ���������գ�ʱ���벻��
	 * @param year ��
	 * @param month ��
	 * @param day ��
	 */
	public void set(int year, int month, int day)
	{
		Calendar theTime = Calendar.getInstance();
		theTime.set(year, month - 1, day);
		set(theTime);
	}

	/**
	 * ���ö��������
	 * @param theTime Calendar����
	 */
	public void set(Calendar theTime)
	{
		WEEK_OF_YEAR   = theTime.get(Calendar.WEEK_OF_YEAR);
		WEEK_OF_MONTH  = theTime.get(Calendar.WEEK_OF_MONTH);
		WEEKDAY        = (theTime.get(Calendar.DAY_OF_WEEK) - 1) == 0 ? 7 : (theTime.get(Calendar.DAY_OF_WEEK) - 1);
		YEAR           = theTime.get(Calendar.YEAR);
		MONTH          = theTime.get(Calendar.MONTH) + 1;
		DAY            = theTime.get(Calendar.DAY_OF_MONTH);
		HOUR           = theTime.get(Calendar.HOUR_OF_DAY);
		MINUTE         = theTime.get(Calendar.MINUTE);
		SECOND         = theTime.get(Calendar.SECOND);
	}
	/**
	 * ����ʱ��
	 * @param time��YYYYMMDD, YYYYMMDDHH, YYYYMMDDHHMM, YYYYMMDDHHMMSS
	 */
	public void set(String time) throws SmafException
	{
		try
		{
			int year   = 0;
			int month  = 0;
			int day    = 0;
			int hour   = 0;
			int minute = 0;
			int second = 0;
			switch (time.length())
			{
				case 8 : //YYYYMMDD
					year   = Integer.parseInt(time.substring(0,4));
					month  = Integer.parseInt(time.substring(4,6));
					day    = Integer.parseInt(time.substring(6,8));
					set(year, month, day);
					return;
				case 10 : //YYYYMMDDHH
					year   = Integer.parseInt(time.substring(0,4));
					month  = Integer.parseInt(time.substring(4,6));
					day    = Integer.parseInt(time.substring(6,8));
					hour   = Integer.parseInt(time.substring(8,10));
					break;
				case 12 : //YYYYMMDDHHMM
					year   = Integer.parseInt(time.substring(0,4));
					month  = Integer.parseInt(time.substring(4,6));
					day    = Integer.parseInt(time.substring(6,8));
					hour   = Integer.parseInt(time.substring(8,10));
					minute = Integer.parseInt(time.substring(10,12));
					break;
				case 14 : //YYYYMMDDHHMMSS
					year   = Integer.parseInt(time.substring(0,4));
					month  = Integer.parseInt(time.substring(4,6));
					day    = Integer.parseInt(time.substring(6,8));
					hour   = Integer.parseInt(time.substring(8,10));
					minute = Integer.parseInt(time.substring(10,12));
					second = Integer.parseInt(time.substring(12,14));
					break;
				default :
			}
			set(year, month, day, hour, minute, second);	
		}
		catch(NumberFormatException e)
		{
			throw new SmafException("����ʱ����ݴ���");
		}
	}
	/**
	 * �õ�����ڼ���
	 * @return ����ڼ���
	 */
	public int getWeekOfYear()
	{
		return WEEK_OF_YEAR;
	}

	/**
	 * �õ����µڼ���
	 * @return ���µڼ���
	 */
	public int getWeekOfMonth()
	{
		return WEEK_OF_MONTH;
	}

	/**
	 * �õ����ڼ� : monday is 1 and sunday is 7
	 * @return ���ڼ�
	 */
	public int getWeekDay()
	{
		return WEEKDAY;
	}

	/**
	 * �õ����
	 * @return ��
	 */
	public int getYear()
	{
		return YEAR;
	}

	/**
	 * �õ�����ַ�
	 * @return ��
	 */
	public String getYearStr()
	{
		return String.valueOf(YEAR);
	}

	/**
	 * �õ��·�
	 * @return ��
	 */
	public int getMonth()
	{
		return MONTH;
	}

	/**
	 * �õ��·��ַ�����}λ��0
	 * @return ��
	 */
	public String getMonthStr()
	{
		return HandleDate.toZeroStr(MONTH);
	}

	/**
	 * �õ���
	 * @return ��
	 */
	public int getDay()
	{
		return DAY;
	}


	/**
	 * �õ����ַ�����}λ��0
	 * @return ��
	 */
	public String getDayStr()
	{
		return HandleDate.toZeroStr(DAY);
	}

	/**
	 * �õ�Сʱ
	 * @return Сʱ
	 */
	public int getHour()
	{
		return HOUR;
	}

	/**
	 * �õ�Сʱ�ַ�����}λ��0
	 * @return Сʱ
	 */
	public String getHourStr()
	{
		return HandleDate.toZeroStr(HOUR);
	}


	/**
	 * �õ�����
	 * @return ����
	 */
	public int getMinute()
	{
		return MINUTE;
	}

	/**
	 * �õ������ַ�����}λ��0
	 * @return ����
	 */
	public String getMinuteStr()
	{
		return HandleDate.toZeroStr(MINUTE);
	}

	/**
	 * �õ���
	 * @return ��
	 */
	public int getSecond()
	{
		return SECOND;
	}

	/**
	 * �õ����ַ�����}λ��0
	 * @return ��
	 */
	public String getSecondStr()
	{
		return HandleDate.toZeroStr(SECOND);
	}

	/**
	 * �õ����ڵ�'YYYYMMDD'��ʽ�ַ�
	 * @return ���ڵ�'YYYYMMDD'��ʽ�ַ�
	 */
	public String getDate()
	{
		return getYearStr() + getMonthStr() + getDayStr();
	}

	/**
	 * �õ�ʱ���'HHMMSS'��ʽ�ַ�
	 * @return ʱ���'HHMMSS'��ʽ�ַ�
	 */
	public String getTime()
	{
		return getHourStr() + getMinuteStr() + getSecondStr();
	}

	/**
	 * �õ�����ʱ���'YYYYMMDDHHMMSS'��ʽ�ַ�
	 * @return ����ʱ���'YYYYMMDDHHMMSS'��ʽ�ַ�
	 */
	public String getDateTime()
	{
		return getDate() + getTime();
	}

	/**
	 * �õ����ڵ�'YYYY��MM��DD��'��ʽ�ַ�
	 * @return ���ڵ�'YYYY��MM��DD��'��ʽ�ַ�
	 */
	public String getDateWithName()
	{
		return getYearStr() + "��" + getMonthStr() + "��" + getDayStr() + "��";
	}

	/**
	 * �õ�ʱ���'HHʱMM��SS��'��ʽ�ַ�
	 * @return ʱ���'HHʱMM��SS��'��ʽ�ַ�
	 */
	public String getTimeWithName()
	{
		return getHourStr() + "ʱ" + getMinuteStr() + "��" + getSecondStr() + "��";
	}

	/**
	 * �õ�����ʱ���'YYYY��MM��DD��HHʱMM��SS��'��ʽ�ַ�
	 * @return : �ַ�
	 */
	public String getDateTimeWithName()
	{
		return getDateWithName() + getTimeWithName();
	}

	/**
	 * �õ����ڵ�'YYYY-MM-DD'��ʽ�ַ�
	 * @return ���ڵ�'YYYY-MM-DD'��ʽ�ַ�
	 */
	public String getDateWithSep()
	{
		return getDateWithSep("-");
	}

	/**
	 * �õ�ʱ���'HH:MM:SS'��ʽ�ַ�
	 * @return ʱ���'HH:MM:SS'��ʽ�ַ�
	 */
	public String getTimeWithSep()
	{
		return getTimeWithSep(":");
	}

	/**
	 * �õ�����ʱ���'YYYY-MM-DD HH:MM:SS'��ʽ�ַ�
	 * @return ����ʱ���'YYYY-MM-DD HH:MM:SS'��ʽ�ַ�
	 */
	public String getDateTimeWithSep()
	{
		return getDateWithSep() + " " +  getTimeWithSep();
	}

	/**
	 * �õ�������sep�ָ��'YYYYsepMMsepDD'��ʽ���ַ�
	 * @param sep �ָ��
	 * @return ������sep�ָ��'YYYYsepMMsepDD'��ʽ���ַ�
	 */
	public String getDateWithSep(String sep)
	{
		return getYearStr() + sep + getMonthStr() + sep + getDayStr();
	}

	/**
	 * �õ�ʱ����sep�ָ��'HHsepMMsepSS'��ʽ���ַ�
	 * @param sep �ָ��
	 * @return ʱ����sep�ָ��'HHsepMMsepSS'��ʽ���ַ�
	 */
	public String getTimeWithSep(String sep)
	{
		return getHourStr() + sep + getMinuteStr() + sep + getSecondStr();
	}

	/**
	 * �õ�������sepdate�ָʱ����septime�ָ����ʱ��֮����sep�ָ��<br>
	 * 'YYYYsepdateMMsepdateDDsepHHseptimeMMseptimeSS'��ʽ���ַ�
	 * @param sepdate ���ڼ�ķָ��
	 * @param sep ���ں�ʱ��֮��ķָ��
	 * @param septime ʱ���ķָ��
	 * @return ��������ʽ������ַ�
	 */
	public String getDateTimeWithSep(String sepdate, String sep, String septime)
	{
		return getDateWithSep(sepdate) + sep +  getTimeWithSep(septime);
	}

	/**
	 * ����ڼ���
	 */
	private int WEEK_OF_YEAR;

	/**
	 * ���µڼ���
	 */
	private int WEEK_OF_MONTH;

	/**
	 * ���ڼ�
	 */
	private int WEEKDAY;

	/**
	 * ��
	 */
	private int YEAR;

	/**
	 * ��
	 */
	private int MONTH;

	/**
	 * ��
	 */
	private int DAY;

	/**
	 * ʱ
	 */
	private int HOUR;

	/**
	 * ��
	 */
	private int MINUTE;

	/**
	 * ��
	 */
	private int SECOND;
}
